<?php

class Timer {
	const UNIT_SECOND = 's';
	const UNIT_MINUTE = 'm';
	const UNIT_HOUR = 'h';
	
	public $startTime;
	public $endTime;
	public $unit = 's';
	
	private static $_unitsPercent = array(
		'ms' => -10,
		's' => 1,
		'm' => 100,
		'h' => 1000,
	);

	public function __construct($unit = self::UNIT_SECOND, $autoStart = true) {
		$this->unit = $unit;
		if($autoStart)
			$this->start();
	}
	
	public function start() {
		$this->startTime = mktime();
	}
	
	public function end() {
		$this->endTime = mktime();
		return $this;
	}
	
	public function __toString() {
		$time = $this->endTime - $this->startTime;
		$percent = self::$_unitsPercent[$this->unit];
		return (string)floor($time/$percent);
	}
}

?>